package com.hdl.school.view;

import android.content.Context;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.videolan.libvlc.LibVLC;
import org.videolan.libvlc.MediaPlayer;
import org.videolan.libvlc.Media;

public class IjkVideoView extends FrameLayout {
    private Context mContext;// 上下文
    private SurfaceView mSurfaceView;// 播放视图
    private String mPath = "";// 视频文件地址
    private float cVol = 0.0f;

    private LibVLC libVLC;
    private MediaPlayer mediaPlayer;
    private boolean isLooping = false;
    private Object mVideoPlayerListener;

    public IjkVideoView(@NonNull Context context) {
        super(context);
        initVideoView(context);
    }

    public IjkVideoView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initVideoView(context);
    }

    public IjkVideoView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initVideoView(context);
    }

    public IjkVideoView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initVideoView(context);
    }

    private void initVideoView(Context context) {
        mContext = context;
        setFocusable(true);
        initSurfaceView();

        // 初始化 VLC
        libVLC = new LibVLC(mContext);
        mediaPlayer = new MediaPlayer(libVLC);
    }

    public void setPath(String path) {
        mPath = path;
        loadVideo();
    }

    private void initSurfaceView() {
        mSurfaceView = new SurfaceView(mContext);
        mSurfaceView.getHolder().addCallback(new LmnSurfaceCallback());
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,
                Gravity.CENTER);
        mSurfaceView.setLayoutParams(layoutParams);
        this.addView(mSurfaceView);
    }

    public Long getCurrentPosition() {
        if (mediaPlayer != null) {
            try{
                return mediaPlayer.getTime();
            }catch (Exception e){
                return 0L;
            }

        } else {
            return null;
        }

    }


    public void setRepeat(boolean loop) {
        this.isLooping = loop;

    }

    public long getLength() {
        if (mediaPlayer != null) {
            return mediaPlayer.getLength();
        } else {
            throw new IllegalStateException("MediaPlayer is not initialized");
        }
    }
    public void setTime(long currentPosition) {
        if (mediaPlayer != null) {
            mediaPlayer.setTime(currentPosition);
        } else {
            throw new IllegalStateException("MediaPlayer is not initialized");
        }
    }

    private class LmnSurfaceCallback implements SurfaceHolder.Callback {
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            mediaPlayer.getVLCVout().setVideoSurface(holder.getSurface(), holder);
            mediaPlayer.getVLCVout().attachViews();
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            loadVideo();
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            mediaPlayer.getVLCVout().detachViews();
        }
    }

    private void loadVideo() {
        if (mPath != null && !mPath.isEmpty()) {
            Media media = new Media(libVLC, Uri.parse(mPath));
            mediaPlayer.setMedia(media);
            mediaPlayer.play();
        }
    }

    public void release() {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.getVLCVout().detachViews();
            mediaPlayer.release();
            libVLC.release();
        }
    }

    public void setVolume(float volume) {
        cVol = volume;
        if (mediaPlayer != null) {
            mediaPlayer.setVolume((int) (volume * 100)); // volume in percentage (0-100)
        }
    }
    public void start() {
        if (mediaPlayer != null) {
            mediaPlayer.play();
        }
    }

    public void setListener(MediaPlayer.EventListener listener) {
//        if (this.mVideoPlayerListener != null && mediaPlayer != null) {
//            mediaPlayer(this.mVideoPlayerListener);
//        }
        this.mVideoPlayerListener = listener;
        if (mediaPlayer != null) {
            mediaPlayer.setEventListener(listener);
        }


    }

    public void pause() {
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
        }
    }
    public void stop() {
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
        }
    }
    public void reset() {
//        mediaPlayer.release();
        mediaPlayer.stop();
        start();
    }
    public void restart() {
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
//            mediaPlayer.
            start();
        }
    }
    public boolean  isPlaying(){
        return mediaPlayer.isPlaying();
    }

    public long getDuration() {
        if (mediaPlayer != null) {
            return mediaPlayer.getLength();
        } else {
            throw new IllegalStateException("MediaPlayer is not initialized");
        }
    }

    public void seekTo(long position) {
        if (mediaPlayer != null) {
            mediaPlayer.setTime(position);
        } else {
            throw new IllegalStateException("MediaPlayer is not initialized");
        }
    }

}

